package com.hospital.controller;

import com.hospital.entity.Role;
import com.hospital.service.RoleService;
import com.hospital.util.JsonUtil;
import com.hospital.util.TransformationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色相关controller
 */
@Controller
@CrossOrigin
@RequestMapping("Role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private JsonUtil jsonUtil;

    /**
     * 查所有角色
     *
     * @return 集合中的角色信息
     */
    @RequestMapping("SelectAll")
    @ResponseBody
    public List<Role> SelectAll() {
        List<Role> roles = roleService.SelectAll();
        return roles;
    }

    /**
     * 添加方法
     *
     * @param role 角色对象
     * @return 添加状态
     */
    @RequestMapping("Insert")
    @ResponseBody
    public Boolean Insert(Role role) {
        //汉字转换
        String roleName = TransformationUtil.chineseCharacters(role.getRoleDesc());
        role.setRoleName(roleName);
        //添加操作
        Integer state = roleService.Insert(role);
        //返回状态
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }

    /**
     * 批量删除用户
     *
     * @param id 用户id
     * @return 删除状态
     */
    @RequestMapping("BulkDelete")
    @ResponseBody
    public Boolean BulkDelete(String id) {
        List<Integer> list = new ArrayList<Integer>();
        String[] strs = id.split(",");
        Integer state = 0;
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            state = roleService.BulkDelete(list);
        }
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }
}
